from myelin.Ns import MYELINS, XML_NS
from myelin.server.core.ft import ModelDriver

from nodetypes.formats.mml.Transform import Document, Transform
from nodetypes.formats.mml import Navigator, Profiler


class Document(Document):
    
    newNavigator = Navigator.Navigator
    newProfiler = Profiler.AgentProfiler
    newContentDbDriver = staticmethod(ModelDriver.FromRepo)
    
    def getNodeDocId(self, context):
        id = context.model.dcIdentifier
        return id and id[0].rdf_value or None
        
    def createModes(self, context, node):
        mode = context.mode           
        
        nxa = node.xml_append
        nxc = node.xml_create_element
        
        m = context.request.graphs.keys()
        m.sort()
        
        #baseUrl = context.resourceUri
        #url = context.urlFormatter.formatUrl
        
        for m in m:
            if m == mode:
                nxa(
                    nxc(
                        u'a:mode', MYELINS,
                        attributes = {
                            u'key': unicode(m),
                            u'selected': u'1',
                            #u'href' : url(baseUrl, mode=m),
                        }
                    )
                )
            else:
                nxa(
                    nxc(
                        u'a:mode', MYELINS,
                        attributes = {
                            u'key': unicode(m),
                            #u'href' : url(baseUrl, mode=m),
                        }
                    )
                )
                
        node.rootNode.childNodes[0].xml_set_attribute(
            u'mode',
            mode
        )
        
        return node
            
    def createNav(self, context, node, locale):
        model = context.model           
        
        nxa = node.xml_append
        nxc = node.xml_create_element
        
        nav = self.newNavigator(
            locale,
            model.rdf_initDriver(
                self.newContentDbDriver(
                    context.repo,
                    context.request.graphs[context.mode]
                )
            )
        )
        
        nav.createParentNav(
            context,
            nxa(
                nxc(
                    u'a:parents', MYELINS
                )
            ),
            model
        )        
        
        nav.createTranslationNav(
            context,
            nxa(
                nxc(
                    u'a:translations', MYELINS
                )
            ),
            model
        )
                
        nav.createGroupingNav(
            context,
            nxa(
                nxc(
                    u'a:node-groupings', MYELINS
                )
            ),
            model
        )
        
        return node
    
    def createMeta(self, context, node, locale):
        model = context.model
        defaults = context.defaults
        
        root = defaults.document_root        
        
        nxa = node.xml_append
        nxc = node.xml_create_element
        
        nxa(
            nxc(
                u'a:language', MYELINS,
                content = locale
            )
        )
        
        cap = self.newProfiler(root, locale).createProfile
        
        m_join = model.rdf_join
        for cur in model.dcCreator:
            nxa(
                nxc(
                    u'a:created', MYELINS,
                    content = m_join('dctermsCreated')
                )
            )        
            cap(
                context,
                node,
                cur,
                u'a:creator',
                MYELINS
            )
        
        for cur in model.mLast_Modifier:
            nxa(
                nxc(
                    u'a:modified', MYELINS,
                    content = m_join('dctermsModified')
                )
            )        
            cap(
                context,
                node,
                cur,
                u'a:last-modifier',
                MYELINS
            )        
        
        cur = nxa(
            nxc(
                u'a:keywords', MYELINS,
            )
        )
        
        cxa = cur.xml_append
        cxc = cur.xml_create_element
        for s in model.dcSubject:
            cxa(
                cxc(
                    u'a:subject', MYELINS,
                    content = s.rdf_value
                )
            )
        
        nxa(
            nxc(
                u'a:description', MYELINS,
                content = m_join('dcDescription')
            )
        )
        
        return node
    
        
    def createContent(self, context, node, locale):
        curNode = node.xml_append(
            node.xml_create_element(
                u'a:grouping', MYELINS,
                attributes={
                    u'role' : u'body'
                }
            )
        )
        
        rdf_join = context.model.rdf_join
        curNode.xml_append(
            curNode.xml_create_element(
                u'a:literal', MYELINS,
                attributes = {
                    u'format' : rdf_join('dcFormat',u'|'),
                    (u'xml:lang', XML_NS) : rdf_join('dcLanguage',u'|'),
                },
                content = unicode(
                    context.source.getContent()
                )
            )
        )
        
        return node
    

class Transform(Transform):
    
    newDocument = Document
    
    def process(self, context, source):
        self.setup(context)
            
        context.model = context.nodeType.retrieveModel(
            context
        ).rdf_prefetch()
        
        doc, imt = self.newDocument().createDocument(context, source)
        
        return doc, imt, self.chain or context.chain
    
    
# cleanup
del ModelDriver, \
    Navigator, \
    Profiler